package com.wyg.blog.controller.manage_blog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.model.blog.Tag;
import com.wyg.blog.service.blog.TagService;
import com.wyg.blog.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/6/30 20:29
 */
@RestController
@RequestMapping("/api/tag")
public class TagController {
    private static Logger logger = LoggerFactory.getLogger(TagController.class);
    @Autowired
    TagService tagService;

    @GetMapping("/getAllTags")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<List<Tag>> getAllTags(){
        List<Tag> tags = tagService.selectAllTags();
        return new JsonResult<>(tags,"200","成功获取博客全部标签!");
    }

    @GetMapping("/getSomeTags")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<IPage<Tag>> getSomeTags(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        Page<Tag> page = new Page<>(current,size);
        IPage<Tag> tagIPage = tagService.selectSomeTags(page);
        return new JsonResult<>(tagIPage,"200","分页获取博客全部标签!");
    }

    @GetMapping("/getTag")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Tag> getTag(@RequestParam("name") String name){
        Tag tag = tagService.selectTagByName(name);
        return new JsonResult<>(tag,"200","标签详细信息!");
    }

    @PostMapping("/addTag")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> addTag(@RequestBody Map<String,String> tagInfo){
        Tag tag = new Tag();
        tag.setId(0);
        tag.setTag(tagInfo.get("tag"));
        tag.setCreate_time(new Date());
        tag.setState(true);
        boolean res = tagService.insertTag(tag);
        if(res){
            return new JsonResult<>(1,"200","添加成功!");
        }else {
            return new JsonResult<>(0,"801","添加失败!");
        }
    }

    @PostMapping("/updateTag")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> updateTag(@RequestBody Map<String,String> tagInfo){
        boolean res = tagService.renewTag(Integer.parseInt(tagInfo.get("id")),tagInfo.get("tag"));
        if(res){
            return new JsonResult<>(1,"200","修改成功!");
        }else {
            return new JsonResult<>(0,"802","修改失败!");
        }
    }

    @PostMapping("/updateTagState")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> updateTagState(@RequestBody Map<String,Integer> tagInfo){
        Tag tag = tagService.selectTagById(tagInfo.get("tag_id"));
        boolean res = tagService.renewTagState(tagInfo.get("tag_id"), !tag.isState());
        if(res){
            return new JsonResult<>(1,"200","修改成功!");
        }else {
            return new JsonResult<>(0,"802","修改失败!");
        }
    }

    @PostMapping("deleteTag")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> deleteTag(@RequestParam("tag_id") Integer id){
        boolean res = tagService.removeTag(id);
        if(res){
            return new JsonResult<>(1,"200","删除成功!");
        }else {
            return new JsonResult<>(0,"803","删除失败!");
        }
    }

    @GetMapping("fuzzy")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')")
    public JsonResult<Object> fuzzySearch(@RequestParam("tag_name") String tag_name) {
        logger.warn("tag_name:" + tag_name);
        if (tag_name != "") {
            String param = "%" + tag_name + "%";
            List<Tag> tags = tagService.fuzzySearch(param);
            return new JsonResult<>(tags, "200", "标签信息模糊查询成功!");
        } else {
            Page<Tag> page = new Page<>(1,10);
            IPage<Tag> tagIPage = tagService.selectSomeTags(page);
            return new JsonResult<>(tagIPage,"200","分页获取博客全部标签!");
        }
    }

}
